/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reginAPI;

import data.DataProvider;
import regin.globalKunstner.*;

import entities.Artist;
import entities.Artwork;
import java.util.List;

import regin.lokalkunstner.*;
import regin.vaerk.Vaerk;
import regin.vaerk.VaerkService;
import regin.vaerk.VaerkServicePortType;

/**
 *
 * @author bertram
 */
public class ReginFacade {

    public static final String WS_STRING_NULL = "__WS_REGIN_NULL__";
    public static final Integer WS_INTEGER_NULL = Integer.MIN_VALUE;
    

    public static Artwork getArtwork(int kidID, String token) {
        try {
            token = new Auth().login("jrw", "qweqwe");
        } catch (Exception ex) {
            token = "";
        }
        Artwork work = new Artwork();
        VaerkServicePortType vaerkPort = getVaerkServicePort();
        KunstnerServicePortType kunstnerPort = getLokalKunstnerPort();
        Vaerk vaerk = vaerkPort.load(token, kidID);
        work.setTitel(vaerk.getTitel());
        work.setReginID(kidID);
        work.setType(vaerk.getVaerktypeId());
        //work.getFinished(vaerk.getDateringSlutaar());
        regin.vaerk.WSSoegResultat search = vaerkPort.getAttached(token, kidID, "kunstnere", "", 10, 0, Boolean.TRUE);
        List<Integer> kunstnerIdListe = search.getIdList();
        for (Integer id : kunstnerIdListe) {
            Artist a = new Artist();
            Kunstner k = kunstnerPort.load(token, id);
            //tager højde for regin/kid kunstner navn ting
            String reginName = k.getNavn();
            if (reginName.contains("global:")) {
                reginName = reginName.replaceAll("global: ", "");
            }
            if (reginName.contains(",")) {
                String[] splitName = reginName.split(",");
                a.setFirstName(splitName[1].trim());
                a.setLastName(splitName[0].trim());
            } else if (reginName.contains(" ")) {
                String[] splitName = reginName.split(" ");
                a.setFirstName(splitName[0].trim());
                a.setLastName(splitName[1].trim());
            } else {
                a.setFirstName(reginName.trim());
            }
            //a.setFirstName(splitName[1]);
           // work.setFinished(vaerk.getDateringSlutdato());
            work.setBegan(vaerk.getDateringStartaar());
            work.setFinished(vaerk.getDateringSlutaar());
            //work.setDescription(vaerk.getBeskrivelse());
            work.setOwner(vaerk.getEjer());
            work.setTechnique(vaerk.getMaterialeTeknik());
            //work.setType(vaerk.getVaerktypeId())
            //work.setDescription(vaerk.getSignaturBetegnelse());
            a.setReginID(id);
            work.addArtist(a);

        }

        return work;
    }

    public static Artist getArtist(int kidID, String token) {
        Artist art = new Artist();
        return art;

    }

    private static VaerkServicePortType getVaerkServicePort() {
        VaerkService service = new VaerkService();
        VaerkServicePortType port = service.getVaerkServiceSOAP12PortHttp();
        return port;
    }

    private static regin.globalKunstner.GlobalKunstnerServicePortType getGlobalKunstnerPort() {
        GlobalKunstnerService service = new GlobalKunstnerService();
        GlobalKunstnerServicePortType port = service.getGlobalKunstnerServiceSOAP12PortHttp();
        return port;
    }

    private static KunstnerServicePortType getLokalKunstnerPort() {
        KunstnerService service = new KunstnerService();
        KunstnerServicePortType port = service.getKunstnerServiceSOAP12PortHttp();
        return port;
    }

    private static regin.search.WSSoegResultat searchKunstner(java.lang.String token, regin.search.KunstnerForm searchForm, java.lang.String sort, java.lang.Integer maxHits, java.lang.Integer firstHit, java.lang.Boolean sortAsc) {
        regin.search.AdvancedSearchService service = new regin.search.AdvancedSearchService();
        regin.search.AdvancedSearchServicePortType port = service.getAdvancedSearchServiceSOAP12PortHttp();
        return port.searchKunstner(token, searchForm, sort, maxHits, firstHit, sortAsc);
    }


}
